home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Robocode / robocode-setup-1.0.7.jar / extract.jar / robots / sample / Crazy.java < prev    next >
Encoding:
Java Source  |  2005-02-18  |  2.1 KB  |  85 lines

  1. package sample;
  2. import robocode.*;
  3. /**
  4.  * Crazy - a sample robot by Mathew Nelson
  5.  * 
  6.  * This robot moves around in a crazy pattern
  7.  */
  8. public class Crazy extends AdvancedRobot
  9. {
  10.     boolean movingForward;
  11.     /**
  12.      * run: Crazy's main run function
  13.      */    
  14.     public void run() {
  15.         while (true)
  16.         {
  17.             // Tell the game we will want to move ahead 40000 -- some large number
  18.             setAhead(40000);
  19.             movingForward = true;
  20.             // Tell the game we will want to turn right 90
  21.             setTurnRight(90);
  22.             // At this point, we have indicated to the game that *when we do something*, 
  23.             // we will want to move ahead and turn right.  That's what "set" means.
  24.             // It is important to realize we have not done anything yet!
  25.             // In order to actually move, we'll want to call a method that
  26.             // takes real time, such as waitFor.
  27.             // waitFor actually starts the action -- we start moving and turning.
  28.             // It will not return until we have finished turning.
  29.             waitFor(new TurnCompleteCondition(this));
  30.             // Note:  We are still moving ahead now, but the turn is complete.
  31.             // Now we'll turn the other way...
  32.             setTurnLeft(180);
  33.             // ... and wait for the turn to finish ...
  34.             waitFor(new TurnCompleteCondition(this));
  35.             // ... then the other way ...
  36.             setTurnRight(180);
  37.             // .. and wait for that turn to finish.
  38.             waitFor(new TurnCompleteCondition(this));
  39.             // then back to the top to do it all again
  40.         }
  41.     }
  42.     
  43.     /**
  44.      * onHitWall:  Handle collision with wall.
  45.      */    
  46.     public void onHitWall(HitWallEvent e)
  47.     {
  48.         // Bounce off!
  49.         reverseDirection();
  50.     }
  51.     
  52.     /**
  53.      * reverseDirection: switch from ahead to back & vice versa
  54.      */    
  55.     public void reverseDirection() {
  56.         if (movingForward)
  57.         {
  58.             setBack(40000);
  59.             movingForward = false;
  60.         }
  61.         else
  62.         {
  63.             setAhead(40000);
  64.             movingForward = true;
  65.         }
  66.     }
  67.     
  68.     /**
  69.      * onScannedRobot:  Fire!
  70.      */    
  71.     public void onScannedRobot(ScannedRobotEvent e)
  72.     {
  73.         fire(1);
  74.     }
  75.  
  76.     /**
  77.      * onHitRobot:  Back up!
  78.      */    
  79.     public void onHitRobot(HitRobotEvent e)
  80.     {
  81.         // If we're moving the other robot, reverse!
  82.         if (e.isMyFault())
  83.             reverseDirection();
  84.     }
  85. }